/*CLIENTAMF.NET - an open-source .NET alternative to Macromedia's .NET Flash Remoting Client
Copyright (C) 2010 Kiko Qiu - http://hi.baidu.com/kikoqiu

This project is based on the open-source project of AMF.NET, 
Part of the code is just a copy from it. You may check it out from sourceforge.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA*/
using System.Net;
using System.IO;
using System.Text;
using System;

namespace kikoqiu
{
    public class HttpHelper
    {        
        public string UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.8.1.12) Gecko/20080219 Firefox/2.0.0.12 Navigator/9.0.0.6";
        public string Accept = @"text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
        public CookieContainer cc = new CookieContainer();
        public string Referer = null;

        private HttpWebRequest HttpInit(string uri)
        {
            HttpWebRequest wr = HttpWebRequest.Create(uri) as HttpWebRequest;
            wr.UserAgent=UserAgent;
            wr.Accept =Accept;            
            wr.CookieContainer = cc;
            if (Referer != null)
            {
                wr.Referer = Referer;
            }
            return wr;
        }
        public delegate long RequestStreamCb(Stream Request);
        public HttpWebResponse HttpPost(string uri, string contentType, RequestStreamCb cb)
        {
            HttpWebRequest wr = HttpInit(uri);
            wr.Method = "POST";
            wr.ContentType = contentType;
            Stream rst= wr.GetRequestStream();
            wr.ContentLength=cb(rst);
            return wr.GetResponse() as HttpWebResponse;
        }
        public HttpWebResponse HttpPost(string uri, string contentType, byte[] content)
        {
            HttpWebRequest wr = HttpInit(uri);            
            wr.Method = "POST";
            wr.ContentType = contentType;
            wr.ContentLength = content.Length;
            wr.GetRequestStream().Write(content, 0, content.Length);
            return wr.GetResponse() as HttpWebResponse;
        }        
        public HttpWebResponse HttpPostForm(string uri, string escapedData,Encoding ec)
        {
            byte[] cnt = ec.GetBytes(escapedData);
            return HttpPost(uri, "application/x-www-form-urlencoded", cnt) as HttpWebResponse;
        }
        public HttpWebResponse HttpPostForm(string uri, string escapedData)
        {
            return HttpPostForm(uri, escapedData, Encoding.UTF8);       
        }
        public HttpWebResponse HttpGet(string uri)
        {
            HttpWebRequest wr = HttpInit(uri);
            wr.Method = "GET";
            return wr.GetResponse() as HttpWebResponse;
        }

        private static System.Collections.Generic.Dictionary<char, string> escaped = new System.Collections.Generic.Dictionary<char, string>();
        static HttpHelper()
        {
            escaped['%']="%25";
            escaped['+']="%2B";
            escaped[' ']="+";

            escaped['@']="%40";
            escaped[':']="%3A";
            escaped['/']="%2F";
            escaped['&']="%26";
            escaped['\\']="%5C";
            escaped['=']="%3D";
            escaped['<']="%3C";
            escaped['>']="%3E";
        }
        public static string Escape(string data)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i != data.Length; ++i)
            {
                if (escaped.ContainsKey(data[i]))
                {
                    sb.Append(escaped[data[i]]);
                }
                else
                {
                    sb.Append(data[i]);
                }
            }
            return sb.ToString();
        }
        public static string GetString(HttpWebResponse res)
        {
            return GetString(res, Encoding.UTF8);
        }
        public static string GetString(HttpWebResponse res, Encoding ed)
        {
            if (res.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("Bad status code " + res.StatusCode);
            }
            System.IO.StreamReader sr = new System.IO.StreamReader(res.GetResponseStream(), ed);
            return sr.ReadToEnd();
        }
    }
}